package com.itgm.jiayun_web_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgm.jiayun_web_project.mapper.AdminMapper;
import com.itgm.jiayun_web_project.pojo.Admin;
import com.itgm.jiayun_web_project.pojo.LoginInfo;
import com.itgm.jiayun_web_project.pojo.PageResult;
import com.itgm.jiayun_web_project.pojo.Result;
import com.itgm.jiayun_web_project.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public LoginInfo login(Admin admin) {
        //获取用户名和密码
        String username = admin.getUsername();
        String password = admin.getPassword();
        log.info("用户名：{}，密码：{}", username, password);
        //密码进行md5加密
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        log.info("加密后的密码：{}", s);
        admin.setPassword(s);
        //查询数据库中是否有该用户
        LambdaQueryWrapper<Admin> qw = new LambdaQueryWrapper<>(); //创建条件构造器
        qw.eq(Admin::getUsername, username);
        Admin result = this.getOne(qw); //查询到的 查询单条数据
        if (result == null) {
            log.info("用户名不存在");
            return null;
        }
        if (!result.getPassword().equals(admin.getPassword())) {
            log.info("密码错误");
            return null;
        }
        if (result.getStatus() == 0){
            log.info("用户被禁用");
            return null;
        }
        log.info("登录成功");
        //生成token
//        HashMap<String, Object> claims = new HashMap<>();
//        claims.put("id", result.getId());
//        claims.put("username", result.getUsername());
//        String token = JwtUtils.generateToken(claims);
        //封装登录信息
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setUsername(result.getUsername());
        loginInfo.setId(result.getId());
//        loginInfo.setUsername(result.getUsername());
        loginInfo.setName(result.getName());
//        loginInfo.setToken(token);
        return loginInfo;
    }

    @Override
    public Result<PageResult<Admin>> page(Integer page, Integer pageSize, String name) {
        //构建分页查询条件
        Page<Admin> page1 = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Admin> qw = new LambdaQueryWrapper<>();
        if (name != null && !name.equals("")){
            qw.like(Admin::getName, name);
        }
        //分页查询
        Page<Admin> page2 = adminMapper.selectPage(page1, qw);
        PageResult<Admin> adminPageResult = new PageResult<>();
        adminPageResult.setTotal(page2.getTotal());
        adminPageResult.setRecords(page2.getRecords());
        return Result.success(adminPageResult);
    }


}
